/*
 * Preview 精彩绘本抢先看
 */

import Preview from 'constants/preview';
import { CALL_BOOK_API } from '../middlewares';

export function createPreview(data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Preview.CREATE, Preview.CREATE_SUCCESS, Preview.CREATE_FAIL],
      uri: '/course-api/a/guides',
      method: 'POST',
      body: data,
    },
  };
}

export function updatePreview(previewId: number, data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Preview.UPDATE, Preview.UPDATE_SUCCESS, Preview.UPDATE_FAIL],
      uri: `/course-api/a/guides/${previewId}`,
      method: 'PUT',
      body: data,
    },
  };
}

export function removePreview(previewId: number) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.UPDATE, Preview.UPDATE_SUCCESS, Preview.UPDATE_FAIL],
      uri: `/course-api/a/guides/${previewId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 查询Preview列表
 * @param filters Preview查询过滤
 */
export function fetchPreviewList(filters: Record<string, any> = {}) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [Preview.FETCH_LIST, Preview.FETCH_LIST_SUCCESS, Preview.FETCH_LIST_FAIL],
      uri: '/course-api/a/guides',
      method: 'GET',
      body: filters,
    },
  };
}

// 更新绘本抢先看的发布状态
// status: 0:不发布，1:发布
export function publishPreview(previewId: number, status: number) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.PUBLISH, Preview.PUBLISH_SUCCESS, Preview.PUBLISH_FAIL],
      uri: `/course-api/a/guides/${previewId}/publish`,
      method: 'PUT',
      body: {
        status,
      },
    },
  };
}

/**
 * 绘本抢先看绘本页面列表
 * @param previewId 抢先看ID
 */
export function fetchPageList(previewId: number) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [
        Preview.FETCH_PAGE_LIST,
        Preview.FETCH_PAGE_LIST_SUCCESS,
        Preview.FETCH_PAGE_LIST_FAIL,
      ],
      uri: `/course-api/a/guides/${previewId}/pages`,
      method: 'GET',
    },
  };
}

/**
 * 创建page
 * @param previewId
 * @param data
 */
export function createPage(previewId: number, data: Record<string, any>) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.CREATE_PAGE, Preview.CREATE_PAGE_SUCCESS, Preview.CREATE_PAGE_FAIL],
      uri: `/course-api/a/guides/${previewId}/pages`,
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 页面详情
 * @param previewId
 * @param pageId
 */
export function fetchPageDetail(previewId: number, pageId: number) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.FETCH_PAGE, Preview.FETCH_PAGE_SUCCESS, Preview.FETCH_PAGE_FAIL],
      uri: `/course-api/a/guides/${previewId}/pages/${pageId}`,
      method: 'GET',
    },
  };
}

/**
 * 保存Page页面
 * @param previewId 课时ID
 * @param pageId 页面ID
 * @param data 页面数据
 */
export function savePage(previewId: number, pageId: number, data: Record<string, any>) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.UPDATE_PAGE, Preview.UPDATE_PAGE_SUCCESS, Preview.UPDATE_PAGE_FAIL],
      uri: `/course-api/a/guides/${previewId}/pages/${pageId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 删除Page页面
 * @param previewId 课时ID
 * @param pageId 页面ID
 * @param data 页面数据
 */
export function removePage(previewId: number, pageId: number) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.REMOVE_PAGE, Preview.REMOVE_PAGE_SUCCESS, Preview.REMOVE_PAGE_FAIL],
      uri: `/course-api/a/guides/${previewId}/pages/${pageId}`,
      method: 'DELETE',
    },
  };
}

export function sortPages(previewId: number, pageIds: number[]) {
  return {
    previewId,
    [CALL_BOOK_API]: {
      types: [Preview.SORT_PAGE, Preview.SORT_PAGE_SUCCESS, Preview.SORT_PAGE_FAIL],
      uri: `/course-api/a/guides/${previewId}/pages-sequence`,
      method: 'PUT',
      body: {
        pageIds,
      },
    },
  };
}
